package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import building.InfoGame;

public class SettingsPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	public GraphicInterface mainFrame;

	private JPanel dimensionPanel;
	private JSpinner sizeField;
	private JLabel sizeLabel;

	private JPanel commandPanel;
	private JButton refreshButton;
	private JButton rButton;
	JButton controlButton;
	JButton randomButton;

	private JPanel statusPanel;
	private JLabel firePhase;
	private JLabel firePhasesValue;
	private JLabel stepsLabel;
	private JLabel stepsValueLabel;
	private JLabel currEnergyLabel;
	private JLabel currEnergyValueLabel;

	private int max_dim = 12;
	private int min_dim = 2;

	private JLabel envTypeLabel;
	private JComboBox envTypeCombobox;

	ImageIcon playIcon, stopIcon, nextIcon, refreshIcon;

	private JButton onesButton;
	private JLabel currVisibilityLabel;
	private JLabel currVisibilityValueLabel;
	private JLabel elapsedTimelabel;
	private JLabel elapsedTimeValueLabel;

	private JButton resetButton;

	private JLabel goalReachedLabel;

	/**** cate ****/
	private InfoGame game;

	private JLabel sizeColLabel;

	private JSpinner sizeColField;

	private JLabel sizeRowLabel;

	private JSpinner sizeRowField;

	private GregorianCalendar startTime;

	private JButton saveB;

	private JButton loadB;

	private JLabel overCLabel;

	private JLabel overCLabelValueLabel;

	private JLabel overCValueLabel;

	/**
	 * 
	 * @param game
	 * @param graphicInterface
	 */
	public SettingsPanel(final InfoGame game, GraphicInterface graphicInterface) {

		this.game = game;
		this.mainFrame = graphicInterface;
		this.startTime = null;

		playIcon = ImageLoader.playIcon;
		stopIcon = ImageLoader.stopIcon;
		nextIcon = ImageLoader.nextIcon;

		refreshIcon = ImageLoader.refreshIcon;

		GridBagLayout jPanel2Layout = new GridBagLayout();
		jPanel2Layout.rowWeights = new double[] { 0.1, 0.1, 0.1, 0.1 };
		jPanel2Layout.rowHeights = new int[] { 1, 1, 1, 1 };
		jPanel2Layout.columnWeights = new double[] { 0.1 };
		jPanel2Layout.columnWidths = new int[] { 1 };

		setLayout(jPanel2Layout);

		dimensionPanel = new JPanel();
		dimensionPanel.setPreferredSize(new Dimension(300, 100));
		Border marginOutside = new EmptyBorder(10, 10, 10, 10);
		TitledBorder title = BorderFactory.createTitledBorder("Map menu");
		CompoundBorder upperBorder = new CompoundBorder(marginOutside, title);
		Border marginInside = new EmptyBorder(10, 10, 10, 10);
		dimensionPanel.setBorder(new CompoundBorder(upperBorder, marginInside));


        //	Start simulation of agent
        commandPanel = new JPanel();
        commandPanel.setPreferredSize(new Dimension(300,110));
        Border marginOutside1 = new EmptyBorder(10,10,10,10);
        TitledBorder title1 = BorderFactory.createTitledBorder("Commands");
        CompoundBorder upperBorder1 = new CompoundBorder(marginOutside1, title1);
        Border marginInside1 = new EmptyBorder(10,10,10,10);
        commandPanel.setBorder(new CompoundBorder(upperBorder1, marginInside1));

        add(commandPanel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.WEST, new Insets(0, 0, 0, 0), 0, 0));

        controlButton = new JButton();
        commandPanel.add(controlButton);
        controlButton.setIcon(playIcon);
        controlButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent arg0)
            {
                System.out.println("Clicked  start:" + mainFrame.start);
              
               //Sound myAlarm= new Sound("veloce-alarm.wav");
                //myAlarm.playSound();
             
             
            
                mainFrame.setStart(true);
                startTime = new GregorianCalendar();
                mainFrame.jGamePanel.repaint();
                class myThread implements Runnable
                {
                    public void run()
                    {
                        mainFrame.mainLoopOnes();

                        try
                        {
                            Thread.sleep(1000);
                        }
                        catch (InterruptedException e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
                new Thread(new myThread()).start();

            }


        });
        randomButton = new JButton();
        commandPanel.add(randomButton);
        randomButton.setIcon(refreshIcon);
        randomButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent arg0)
            {
                System.out.println("Random Config:");
             
                try {

					game.shuffleAgents();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				mainFrame.jGamePanel.repaint();

			}

		});

		{
			statusPanel = new JPanel();
			Border marginOutside2 = new EmptyBorder(10, 10, 10, 10);
			TitledBorder title2 = BorderFactory.createTitledBorder("Status");
			CompoundBorder upperBorder2 = new CompoundBorder(marginOutside2,
					title2);
			Border marginInside2 = new EmptyBorder(10, 10, 10, 10);
			statusPanel.setBorder(new CompoundBorder(upperBorder2,
					marginInside2));
			statusPanel.setLayout(new GridLayout(5, 1));
			add(statusPanel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
					GridBagConstraints.WEST, GridBagConstraints.WEST,
					new Insets(0, 0, 0, 0), 0, 0));

			JPanel fpPanel = new JPanel();
			JPanel currTypeEnvPanel = new JPanel();
			statusPanel.add(currTypeEnvPanel);
			{
				elapsedTimelabel = new JLabel("Elapsed Time:");
				elapsedTimeValueLabel = new JLabel("" + mainFrame.showTime());

				currTypeEnvPanel.add(elapsedTimelabel);
				currTypeEnvPanel.add(elapsedTimeValueLabel);
			}
			statusPanel.add(fpPanel);
			{
				firePhase = new JLabel("Fire Phase :");
				firePhasesValue = new JLabel("" + mainFrame.getFirePhase());
				fpPanel.add(firePhase);
				fpPanel.add(firePhasesValue);
			}
			JPanel stepPanel = new JPanel();
			statusPanel.add(stepPanel);
			{
				stepsLabel = new JLabel("Turn Number:");
				stepsValueLabel = new JLabel("" + mainFrame.getNTurn());
				stepPanel.add(stepsLabel);
				stepPanel.add(stepsValueLabel);
			}

			JPanel overCPanel = new JPanel();
			statusPanel.add(overCPanel);
			{
				overCLabel = new JLabel("Overcrowded :");

				overCValueLabel = new JLabel();
				if (game.getInfo().isOvercrowded(game)) {
					overCValueLabel.setText("Yes");
				} else {
					overCValueLabel.setText("No");
				}
				overCPanel.add(overCLabel);
				overCPanel.add(overCValueLabel);
			}

			JPanel goalPanel = new JPanel();
			statusPanel.add(goalPanel);
			{
				goalReachedLabel = new JLabel("GAME NOT FINISHED");
				goalReachedLabel.setForeground(Color.RED);
				goalPanel.add(goalReachedLabel);
			}
		}
	}

	public void update() {
		elapsedTimeValueLabel.setText("" + mainFrame.showTime());
		firePhasesValue.setText("" + mainFrame.getFirePhase());
		stepsValueLabel.setText("" + mainFrame.getNTurn());

		if (mainFrame.isGameFinished()) {
			goalReachedLabel.setForeground(Color.GREEN);
			goalReachedLabel.setText("GAME FINISHED");
		} else {
			goalReachedLabel.setForeground(Color.RED);
			goalReachedLabel.setText("GAME NOT FINISHED");
		}

		overCValueLabel = new JLabel();
		if (game.getInfo().isOvercrowded(game)) {
			overCValueLabel.setText("Yes");
		} else {
			overCValueLabel.setText("No");
		}

	}

	public InfoGame getGame() {
		// TODO Auto-generated method stub
		return null;
	}

}